<template>
  <div>
   <van-datetime-picker
  v-model="currentDate"
  type="date"
  title="选择年月日"
  :min-date="minDate"
  :max-date="maxDate"
  item-height="30"
   @confirm="onConfirm"
  @cancel="onCancel"
/>
  </div>
</template>

<script>
import dayjs from 'dayjs'
// 加载中文语言包
import 'dayjs/locale/zh-cn'
import { changeUser } from '@/api/user'
export default {
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      minDate: new Date(1965, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date(this.value)
    }
  },
  methods: {
    async onConfirm () {
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        loadingType: 'spinner',
        duration: 0
      })
      try {
        await changeUser({
          birthday: dayjs(this.currentDate).format('YYYY-MM-DD')
        })
        this.$toast('修改成功')
        this.$emit('input', dayjs(this.currentDate).format('YYYY-MM-DD'))
        this.$emit('close')
      } catch (err) {
        this.$toast('修改失败', err)
      }
    },
    onCancel () {
      this.$toast('取消')
      this.$emit('close')
    }
  }
}
</script>

<style>

</style>
